package com.cxj.cxj_test_cloud.base.mvvm.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.DefaultLifecycleObserver;

import com.cxj.cxj_test_cloud.base.mvvm.model.BaseModel;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class BaseViewModel<M extends BaseModel> extends AndroidViewModel implements DefaultLifecycleObserver, Consumer<Disposable> {
    @Inject
    protected M mModel;
    protected CompositeDisposable mCompositeDisposable;

    public BaseViewModel(@NonNull Application application) {
        super(application);
        mCompositeDisposable = new CompositeDisposable();
    }

    @Override
    public void accept(Disposable disposable) throws Exception {
        if (mCompositeDisposable != null){
            mCompositeDisposable.add(disposable);
        }
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (mModel !=null){
            mModel.onCleared();
        }

        if (mCompositeDisposable != null){
            mCompositeDisposable.clear();
        }
    }


}
